home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / ReplicateScaleFilter.java < prev    next >
Text File  |  1998-09-22  |  6KB  |  176 lines

  1. /*
  2.  * @(#)ReplicateScaleFilter.java    1.3 98/07/01
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.awt.image;
  16.  
  17. import java.awt.image.ImageConsumer;
  18. import java.awt.image.ColorModel;
  19. import java.util.Hashtable;
  20. import java.awt.Rectangle;
  21.  
  22. /**
  23.  * An ImageFilter class for scaling images using the simplest algorithm.
  24.  * This class extends the basic ImageFilter Class to scale an existing
  25.  * image and provide a source for a new image containing the resampled
  26.  * image.  The pixels in the source image are sampled to produce pixels
  27.  * for an image of the specified size by replicating rows and columns of
  28.  * pixels to scale up or omitting rows and columns of pixels to scale
  29.  * down.
  30.  * It is meant to be used in conjunction with a FilteredImageSource
  31.  * object to produce scaled versions of existing images.
  32.  *
  33.  * @see FilteredImageSource
  34.  * @see ImageFilter
  35.  *
  36.  * @version    1.3 07/01/98
  37.  * @author     Jim Graham
  38.  */
  39. public class ReplicateScaleFilter extends ImageFilter {
  40.     protected int srcWidth;
  41.     protected int srcHeight;
  42.     protected int destWidth;
  43.     protected int destHeight;
  44.  
  45.     protected int srcrows[];
  46.     protected int srccols[];
  47.     protected Object outpixbuf;
  48.  
  49.     /**
  50.      * Constructs a ReplicateScaleFilter that scales the pixels from
  51.      * its source Image as specified by the width and height parameters.
  52.      * @param width the target width to scale the image
  53.      * @param height the target height to scale the image
  54.      */
  55.     public ReplicateScaleFilter(int width, int height) {
  56.     destWidth = width;
  57.     destHeight = height;
  58.     }
  59.  
  60.     /**
  61.      * Passes along the properties from the source object after adding a
  62.      * property indicating the scale applied.
  63.      */
  64.     public void setProperties(Hashtable props) {
  65.     props = (Hashtable) props.clone();
  66.     String key = "rescale";
  67.     String val = destWidth + "x" + destHeight;
  68.     Object o = props.get(key);
  69.     if (o != null && o instanceof String) {
  70.         val = ((String) o) + ", " + val;
  71.     }
  72.     props.put(key, val);
  73.     super.setProperties(props);
  74.     }
  75.  
  76.     /**
  77.      * Override the dimensions of the source image and pass the dimensions
  78.      * of the new scaled size to the ImageConsumer.
  79.      * @see ImageConsumer
  80.      */
  81.     public void setDimensions(int w, int h) {
  82.     srcWidth = w;
  83.     srcHeight = h;
  84.     if (destWidth < 0) {
  85.         if (destHeight < 0) {
  86.         destWidth = srcWidth;
  87.         destHeight = srcHeight;
  88.         } else {
  89.         destWidth = srcWidth * destHeight / srcHeight;
  90.         }
  91.     } else if (destHeight < 0) {
  92.         destHeight = srcHeight * destWidth / srcWidth;
  93.     }
  94.     consumer.setDimensions(destWidth, destHeight);
  95.     }
  96.  
  97.     private void calculateMaps() {
  98.     srcrows = new int[destHeight + 1];
  99.     for (int y = 0; y <= destHeight; y++) {
  100.         srcrows[y] = (2 * y * srcHeight + srcHeight) / (2 * destHeight);
  101.     }
  102.     srccols = new int[destWidth + 1];
  103.     for (int x = 0; x <= destWidth; x++) {
  104.         srccols[x] = (2 * x * srcWidth + srcWidth) / (2 * destWidth);
  105.     }
  106.     }
  107.    
  108.     /**
  109.      * Choose which rows and columns of the delivered byte pixels are
  110.      * needed for the destination scaled image and pass through just
  111.      * those rows and columns that are needed, replicated as necessary.
  112.      */
  113.     public void setPixels(int x, int y, int w, int h,
  114.               ColorModel model, byte pixels[], int off,
  115.               int scansize) {
  116.     if (srcrows == null || srccols == null) {
  117.         calculateMaps();
  118.     }
  119.     int sx, sy;
  120.     int dx1 = (2 * x * destWidth + srcWidth - 1) / (2 * srcWidth);
  121.     int dy1 = (2 * y * destHeight + srcHeight - 1) / (2 * srcHeight);
  122.     byte outpix[];
  123.     if (outpixbuf != null && outpixbuf instanceof byte[]) {
  124.         outpix = (byte[]) outpixbuf;
  125.     } else {
  126.         outpix = new byte[destWidth];
  127.         outpixbuf = outpix;
  128.     }
  129.     for (int dy = dy1; (sy = srcrows[dy]) < y + h; dy++) {
  130.         int srcoff = off + scansize * (sy - y);
  131.         int dx;
  132.         for (dx = dx1; (sx = srccols[dx]) < x + w; dx++) {
  133.         outpix[dx] = pixels[srcoff + sx];
  134.         }
  135.         if (dx > dx1) {
  136.         consumer.setPixels(dx1, dy, dx - dx1, 1,
  137.                    model, outpix, dx1, destWidth);
  138.         }
  139.     }
  140.     }
  141.  
  142.     /**
  143.      * Choose which rows and columns of the delivered int pixels are
  144.      * needed for the destination scaled image and pass through just
  145.      * those rows and columns that are needed, replicated as necessary.
  146.      */
  147.     public void setPixels(int x, int y, int w, int h,
  148.               ColorModel model, int pixels[], int off,
  149.               int scansize) {
  150.     if (srcrows == null || srccols == null) {
  151.         calculateMaps();
  152.     }
  153.     int sx, sy;
  154.     int dx1 = (2 * x * destWidth + srcWidth - 1) / (2 * srcWidth);
  155.     int dy1 = (2 * y * destHeight + srcHeight - 1) / (2 * srcHeight);
  156.     int outpix[];
  157.     if (outpixbuf != null && outpixbuf instanceof int[]) {
  158.         outpix = (int[]) outpixbuf;
  159.     } else {
  160.         outpix = new int[destWidth];
  161.         outpixbuf = outpix;
  162.     }
  163.     for (int dy = dy1; (sy = srcrows[dy]) < y + h; dy++) {
  164.         int srcoff = off + scansize * (sy - y);
  165.         int dx;
  166.         for (dx = dx1; (sx = srccols[dx]) < x + w; dx++) {
  167.         outpix[dx] = pixels[srcoff + sx];
  168.         }
  169.         if (dx > dx1) {
  170.         consumer.setPixels(dx1, dy, dx - dx1, 1,
  171.                    model, outpix, dx1, destWidth);
  172.         }
  173.     }
  174.     }
  175. }
  176.